package org.amphiprion.rfreplayanalyser.ui.race;

import java.awt.Color;
import java.awt.Component;

import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

@SuppressWarnings("serial")
public class DriverTableCellRenderer extends DefaultTableCellRenderer {
	private RaceProvider raceProvider;

	public DriverTableCellRenderer(RaceProvider raceProvider) {
		this.raceProvider = raceProvider;
	}

	@Override
	public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
		DriverItem item = ((DriverListTableModel) table.getModel()).getDriverAtPos(row);
		isSelected = raceProvider.isDriverItemSelected(item);
		super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
		if (column == 0) {
			super.setBackground(item.color);
			if (item.color.getRed() <= 170 && item.color.getGreen() <= 170 && item.color.getBlue() <= 170) {
				super.setForeground(Color.WHITE);
			} else {
				super.setForeground(Color.BLACK);
			}
		} else {
			if (!isSelected) {
				super.setBackground(table.getBackground());
				super.setForeground(table.getForeground());
			}
			if (item.running) {
				super.setForeground(Color.BLACK);
			} else {
				super.setForeground(Color.LIGHT_GRAY);
			}
		}
		return this;
	}
}
